home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4303 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 31 Jan 1996 17:25:27 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4eo8m7$647@ns.RezoNet.NET>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net> <TANMOY.96Jan28100725@qcd.lanl.gov>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In referenced article, Tanmoy Bhattacharya says...
  15. >This issue keeps coming up in this newsgroup, and as any stylistic
  16. >question, will probably never be `finally answered'. On compilers
  17. >which warn on undeclared functions, avoiding the cast has little
  18. >benefit in this particular case. One must keep in mind, however, that
  19. >programming is more a matter of habit: and, often, it is best not to
  20. >become too used to facilities one has at hand on a particular day on a
  21. >particular platform. The compiler _need not_ warn about an undeclared
  22. >function (i.e. such a warning is not mandated by ANSI: no warning is
  23. >however ever prohibited). Converting an int to a pointer without a
  24. >cast, however, needs a warning. On such compilers, then, avoiding the
  25. >cast can often help spot a fatal error.
  26.  
  27. Perhaps I'm falling into the Trap that Tanmoy points out, i.e. relying 
  28. on my tools to give warnings under these circumstances, but I feel very 
  29. strongly that if your compiler does not point out these possible 
  30. errors, then some version of lint should be run on code as part of a 
  31. consistency check.
  32.  
  33. Perhaps the use of such a facility belongs in Peter's recent rules for 
  34. posting in c.l.c.
  35.  
  36. >The point that Ray brings up is also one discussed here quite
  37. >often. The first thing is that writing 
  38. >
  39. >long_ptr = malloc (sizeof (int));
  40. >
  41. >is inherently more error prone than writing
  42. >
  43. >long_ptr = malloc (sizeof*long_ptr);
  44. >
  45. >This second form is useful to learn as an idiom, and avoids most of
  46. >the errors that the explicit cast is trying to flag.
  47.  
  48. I agree that this is the best solution.
  49.  
  50. >A related point is that a `cast' is a loophole in type-safety, just as
  51. >a `goto' is a loophole in structured programming. Both have their
  52. >legitimate uses, and in the hands of an expert programmer, both of
  53. >them can integrate into magnificient products of art. A beginner
  54. >however is often advised to stay away from a goto, I wish the same
  55. >were done for casts. Unfortunately, C does not allow complete
  56. >elimination of casts: but avoiding them whenever possible is probably
  57. >an important lesson that every beginner needs to have.
  58.  
  59. Unfortunately, many uses of "void *" are far bigger loopholes in type 
  60. safety than a cast.  Compilers or lint will pick up most erroneous uses 
  61. of a cast.  A wrong pointer type as the value of a void * function 
  62. parameter can only be debugged at runtime - [a pox on MS for their use 
  63. of general handle manipulating functions with void * parameters (:-(].
  64.  
  65. -- 
  66. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  67. Montreal                       | Phax : (514) 938 5225
  68. ray@ultimate-tech.com          | Home : (514) 630 3749
  69.  
  70.